/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.nageoffer.shortlink.admin.config;

import com.nageoffer.shortlink.admin.common.biz.user.UserContext;
import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * openFeign 微服务调用传递用户信息配置
 */
@Configuration
public class OpenFeignConfiguration {

    /**
     * 配置请求拦截器
     *
     * 本方法旨在为所有的请求添加用户相关的HTTP头部信息
     * 这包括用户名、用户ID和真实姓名，它们来源于用户的上下文信息
     * 这样的设计确保了用户信息能够贯穿整个请求链路，便于在微服务架构中进行用户身份的识别和追踪
     *
     * @return RequestInterceptor 一个请求拦截器实例，用于自动添加用户信息到HTTP请求头部
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return template -> {
            // 添加用户名到HTTP头部
            template.header("username", UserContext.getUsername());
            // 添加用户ID到HTTP头部
            template.header("userId", UserContext.getUserId());
            // 添加用户真实姓名到HTTP头部
            template.header("realName", UserContext.getRealName());
        };
    }
}
